/**
 * 移动地图
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class MoveMap extends cc.Component {

    public static lastPos:cc.Vec2 = null;
    @property(cc.Node)
    mapNode: cc.Node = null;
    private moveSpeend = 0.5;

    @property({tooltip:"是否Y轴移动"})
    isY:boolean = true;

    onLoad(){  
        this.mapNode.on(cc.Node.EventType.TOUCH_MOVE, this.onMapMove,this); 
    }

    onDestroy(){ 
        this.mapNode.off(cc.Node.EventType.TOUCH_MOVE, this.onMapMove,this);
    }

    onMapMove(e:cc.Event.EventTouch){ 
        var delta = e.touch.getDelta(); 
        this.mapNode.x += delta.x *  this.moveSpeend;
        if(this.isY){
            this.mapNode.y += delta.y *  this.moveSpeend;
        }
        this.fixPos(cc.v2(0,0));
    }
 
      //修正坐标
      fixPos(delta){  
        var x = this.mapNode.x+delta.x;
        var y = this.mapNode.y+delta.y;    
        let cW  =  this.mapNode.width  * this.mapNode.scaleX
        let cH  = this.mapNode.height * this.mapNode.scaleY
        var winSize = cc.winSize;
        var xMin = -cW /2 + winSize.width/2;
        var xMax = cW /2  - winSize.width/2;
        var yMin = -cH/2  + winSize.height/2;
        var yMax  = cH/2  - winSize.height/2;  

        x = x  > xMax  ? xMax  :x;
        x = x < xMin  ? xMin  : x;
        y = y > yMax  ? yMax  : y;
        y = y < yMin  ? yMin  :y; 
        MoveMap.lastPos = cc.v2(x,y);  
        this.mapNode.setPosition(cc.v2(x,y))   
    }

}
